import tkinter as tk

from gmssl import sm4

from usertable import *
from usertable import globalVal

def setmainkey(key):
	if len(key.encode('utf-8'))<16:
		puterror_win("总体密钥长度不得小于128位16字节")
	else:
		globalVal.mainkey=key

def setmainkey_win():
	keywin=tk.Toplevel()
	keywin.title("密钥设置")
	keywin.geometry("500x250")
	prompt0=tk.Label(keywin,text="当前采用中国加密算法SM4进行文本消息加密通信")
	mainkeyprompt=tk.Label(keywin,text="总体密钥：（不少于128位16字节）")
	mainkeyin=tk.Text(keywin,width="40",height='3')
	mainkeyin.config(font=('SimSun',15,"italic"),wrap='word')
	mainkeybutton_frame=tk.Frame(keywin)
	mainkeybutton=tk.Button(keywin,text=" 确认 ",command=lambda:setmainkey(mainkeyin.get()))
	
	mainkeyinput_tab=tk.Button(mainkeybutton_frame,text="插入TAB",command=lambda:mainkeyin.insert(tk.END,'\t'))
	mainkeyinput_LF=tk.Button(mainkeybutton_frame,text="插入换行",command=lambda:mainkeyin.insert(tk.END,'\n'))
	mainkeyinput_ring=tk.Button(mainkeybutton_frame,text="插入响铃",command=lambda:mainkeyin.insert(tk.END,'\a'))
	
	prompt0.place(x=10,y=10)
	mainkeyprompt.place(x=10,y=30)
	mainkeyin.place(x=10,y=60)
	mainkeyin.insert('0.0',globalVal.mainkey)
	
	mainkeybutton_frame.place(x=10,y=150)
	mainkeyinput_tab.grid(row=0,column=0)
	mainkeyinput_LF.grid(row=0,column=1)
	mainkeyinput_ring.grid(row=0,column=2)
	mainkeybutton.place(x=430,y=200)
